package com.company.cloud.user.modules.app.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.user.constant.*;
import com.company.cloud.user.modules.api.constant.UserOneSourceEnum;
import com.company.cloud.user.modules.api.constant.UserRegisterWayEnum;
import com.company.cloud.user.modules.api.request.UserMgrUpdateParam;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
import com.company.cloud.user.modules.app.service.UserCompanyService;
import com.company.cloud.user.modules.base.entity.UserInvitationInfo;
import com.company.cloud.user.modules.base.service.UserInfoService;
import com.company.cloud.user.modules.base.service.UserInvitationInfoService;
import com.company.cloud.user.modules.cache.service.UserCompanyCheckService;
import com.company.cloud.user.modules.cache.utils.UserConvertUtils;
import com.company.cloud.user.modules.mgr.controller.req.UserUpdateParam;
import com.company.common.server.config.cloud.ProjectConfig;
import com.company.common.server.config.mq.constant.MQTopicKeys;
import com.company.common.server.config.mq.utils.SaasMqUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.HttpContextUtils;
import com.company.cloud.user.modules.api.request.UserRegisterParam;
import com.company.cloud.user.modules.app.service.UserCodeService;
import com.company.cloud.user.modules.app.service.UserLoginService;
import com.company.cloud.user.modules.app.service.UserRegisterService;
import com.company.cloud.user.modules.app.utils.UserUtils;
import com.company.cloud.user.modules.base.entity.UserInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;

import java.util.HashSet;
import java.util.Set;

@Log4j2
@RestController
@RequestMapping("/app/user/register")
@Api(tags = "User_App_用户注册服务")
public class UserRegisterController {

    @Autowired
    private UserRegisterService userRegisterService;

    @Autowired
    private UserLoginService userLoginService;

    @Autowired
    private UserCodeService userCodeService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserCompanyService userCompanyService;

    @Autowired
    private ProjectConfig projectConfig;

    @PostMapping("/registerByMobileNotLogin")
    @ApiOperation(value = "手机注册-不登录", response = LoginUserInfo.class)
    public BaseResult registerByMobileNotLogin(@RequestBody UserRegisterParam params) {
        String companyNo = userCompanyService.getCompany2Check(AuthContextUtils.getShareCode(), AuthContextUtils.getCompanyNo());
        params.setCompanyNo(companyNo);

        userCodeService.validate(params.getCompanyNo(), UserMsgSendTypeEnum.Reg_VCode, params.getMobile(), params.getVcode(), true);
        UserInfo userInfo = this._register2Mobile(params);
        LoginUserInfo data = UserUtils.getLoginUser(userInfo, null, null, null, null);
        return BaseResult.ok(data);
    }

    @PostMapping("/registerByMobile2Login")
    @ApiOperation(value = "手机注册-登录", response = LoginUserInfo.class)
    public BaseResult registerByMobile2Login(@RequestBody UserRegisterParam params) {
        String companyNo = userCompanyService.getCompany2Check(AuthContextUtils.getShareCode(), AuthContextUtils.getCompanyNo());
        params.setCompanyNo(companyNo);

        userCodeService.validate(params.getCompanyNo(), UserMsgSendTypeEnum.Reg_VCode, params.getMobile(), params.getVcode(), true);
        UserInfo userInfo = this._register2Mobile(params);
        LoginUserInfo data = userLoginService.login(userInfo);
        return BaseResult.ok(data);
    }

    /**
     * 注册信息：手机号、昵称，可以实时同步到API接口
     * @param params
     * @return
     */
    @PostMapping("/ticket/save")
    @ApiOperation(value = "API注册用户")
    public BaseResult ticketRegister(@RequestBody UserRegisterParam params) {
        ValidatorUtils.validate(params);
        params.setSourceOne(UserOneSourceEnum.Data_Import.getCode());
        params.setCompanyNo(params.getCompanyNo());
        this._register2Mobile(params);
        return BaseResult.ok();
    }

    /**
     * 实名信息：姓名、证件类别、证件号码、手机号，做定时推送（默认1小时推送一次）
     * @param params
     * @return
     */
    @PostMapping("/ticket/update")
    @ApiOperation(value = "API更新用户信息")
    public BaseResult ticketUpdate(@RequestBody UserUpdateParam params) {
        ValidatorUtils.validate(params);
        UserInfo userInfo = userLoginService.getRegUserByMobile("PTC", params.getTel());
        if (userInfo == null) {
            return BaseResult.error("用户(手机号)不存在");
        }
        userInfo.setRealName(params.getRealName());
        userInfoService.updateById(userInfo);
        return BaseResult.ok();
    }

    /**
     * 手机注册
     */
    private UserInfo _register2Mobile(UserRegisterParam params) {
        ValidatorUtils.validate(params);
        params.setRegIp(HttpContextUtils.getClientIP());
        params.setRegWay(UserRegisterWayEnum.Reg_Mobile.getCode());
        UserInfo user = userRegisterService.register(params, null);
        return user;
    }


}
